package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.cqut.dto.LimitShowDTO;
import com.cqut.service.IPendingReviewJudgeService;
import com.cqut.util.ResponseUtil;

@RestController
@RequestMapping("/api/pendingReviewJudge")
public class PendingReviewJudgeManagementController {
	
	  @Resource(name="pendingReviewJudgeService")
	  private IPendingReviewJudgeService pendingReviewJudgeService;

	  /**
	   * 初始化列表
	   * @param limitShowDTO
	   * @param errors
	   * @return
	   */
	   @RequestMapping(value = "/list", method = RequestMethod.GET)
	   public ResponseEntity<String> list(
			String judgeName,String subordinate_units,
	   		@Validated
	   		LimitShowDTO limitShowDTO, BindingResult errors) {
		   if(errors.hasErrors())
	           return ResponseUtil.getResponseEntityWhenInvalidReqParams();
	        
	       Map<String, Object> result = new HashMap<String, Object>();
	       List<Map<String, Object>> roleList = pendingReviewJudgeService.findJudgeList(judgeName,subordinate_units,limitShowDTO);
	       long count = pendingReviewJudgeService.findCountOfPendingReviewJudges(judgeName,subordinate_units);
	
	       result.put("rows", roleList);
	       result.put("total", count);
	
	       return ResponseUtil.getResEntityForGetAndJson(result);
	   }
	
	   /**
	    * 审核通过
	    * **/
	   @RequestMapping(value = "/pass", method = RequestMethod.POST)
	   public ResponseEntity<String> paddJudges(
			   @RequestParam("judgesIds")
			   String[] judgesIds){
		   
		   if(judgesIds == null || 0 == judgesIds.length)
				return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		   
		   if(pendingReviewJudgeService.passPendingReviewJudges(judgesIds))
			   return ResponseUtil.getResEntityForPPPWhenSuccess();
		   else 
			   return ResponseUtil.getResEntityForPPPWhenFail();
	   }
			   
	   
	   /**
	    * 审核退回
	    * **/
	   @RequestMapping(value = "/retreat", method = RequestMethod.POST)
	   public ResponseEntity<String> retreatJudges(
			   @RequestParam("judgesIds")
			   String[] judgesIds){
		 
		   if(judgesIds == null || 0 == judgesIds.length)
				return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		   
		   if(pendingReviewJudgeService.retreatPendingReviewJudges(judgesIds))
			   return ResponseUtil.getResEntityForPPPWhenSuccess();
		   else 
			   return ResponseUtil.getResEntityForPPPWhenFail();
	   }
	   
	 /**
	  * 评委详情
	  * **/
	  @RequestMapping(value = "/detail", method = RequestMethod.GET)
	  public ResponseEntity<String> judgeDetail(
			  String judgeId){
		  if(judgeId == null)
				return ResponseUtil.getResponseEntityWhenInvalidReqParams();
	
		  Map<String, Object> role = pendingReviewJudgeService.pendingReviewJudgeDetail(judgeId);
		 
		  return ResponseUtil.getResEntityForGetAndJson(role);
	  }
}
	